<?php
namespace Application\Api\Controller;

class Order extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_ORDER);
    }

    /*
        /Api/Order/getList
    */
    public function getListAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $status = $statusIn = (int)tryGet($data, 'status', 0);
        if(!$status){
            $statusIn = array2string([ORDER_STATUS_NO_PAY, ORDER_STATUS_PAY, ORDER_STATUS_CANCEL, ORDER_STATUS_FINISH]);
        }
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 10);
        $temp = $this->getService()->getList($userId, $statusIn, $page, $limit);
        $list = $temp['list'];
        $productList = [];
        foreach($list as $k => $v){
            $v->status_text = getOrderStatusText($v->dry_status);
            $v->type_text = getPayTypeText($v->dry_type);
            $v->product = json_decode($v->dry_product_raw);
            unset($v->dry_product_raw, $v->dry_pay_raw);
            $v->download = $this->getService()->canDownload($userId, $v, $v->product);
            $list[$k] = $v;
            $productList[] = $v->product;
        }
        $productList = $this->get(SERVICE_FILE)->getFileData($productList, ['dry_image' => 1]);
        $send = [
            'oss_domain' => getOssDomain(),
            'list' => $list
        ];
        $send = addPageInformation($send, $page, $limit, $temp['count']);
        $this->sendSuccess($send);
    }

    /*
        /Api/Order/canDownload
    */
    public function canDownloadAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $id = (int)$data['id'];
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $serviceOrder = $this->getService();
        $order = $serviceOrder->one($id);
        $product = json_decode($order->dry_product_raw);
        $result = $serviceOrder->canDownload($userId, $order, $product);
        $send = [
            'result' => $result,
            'text' => ''
        ];
        if($result == '0'){
            $send['text'] = '暂无详情页';
        }
        $this->sendSuccess($send);
    }

    /*
        /Api/Order/downloadList
    */
    public function downloadListAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $id = (int)$data['id'];
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $serviceOrder = $this->getService();
        $order = $serviceOrder->one($id);
        $product = json_decode($order->dry_product_raw);
        $mainProduct = $product->dry_main_product;
        $time = $serviceOrder->canDownload($userId, $order, $product);
        $send = [
            'list' => [],
            'text' => [
                "1.您可以在{$time}前随时下载源代码",
                '2.每次获取的下载地址只能只用1次',
                '3.请勿传播您的专属下载地址，否则取消下载资格并且不予退款，后果严重的还将追究赔偿。'
            ]
        ];
        $cacheCommon = $this->get(CACHE_COMMON);
        $domain = getDomain();
        $ossDomain = getOssDomain();
        foreach(getDrycmsVipVersionList() as $version){
            $key = md5(time().$version);
            $url = "{$domain}/Api/Order/download?id={$id}&key={$key}";
            $download = "{$ossDomain}/download2020/{$mainProduct}/{$version}";
            $cacheCommon->setData($key, DAY_SECOND, $download);
            $item = [
                'name' => 'DRYCMS',
                'version' => $version,
                'url' => $url
            ];
            $send['list'][] = $item;
        }
        $this->sendSuccess($send);
    }

    /*
        /Api/Order/download
    */
    public function downloadAction()
    {
        $data = $this->getRequestData();
        if(notSetOrEmpty($data, 'id') || notSetOrEmpty($data, 'key')){
            $this->sendFail('PARAMETER_ERROR', '', __LINE__);
            return false;
        }
        $key = $data['key'];
        $cacheCommon = $this->get(CACHE_COMMON);
        $download = $cacheCommon->getData($key);
        if($download != ''){
            $cacheCommon->removeData($key);
            $content = file_get_contents($download);
            $this->downloadZip(basename($download));
            $this->response->end($content);
            return true;
        }
        $this->sendFail('PARAMETER_ERROR', '', __LINE__);
        return false;
    }

    /*
        /Api/Order/isBuy
    */
    public function isBuyAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $productId = (int)$data['productId'];
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $serviceOrder = $this->getService();
        $isBuy = $serviceOrder->isBuy($userId, $productId);
        $this->sendSuccess(['buy' => $isBuy]);
    }

}